mvn  -f pom.xml clean package install -Dmaven.test.skip=true

#version=$(date +%Y%m%d%H%M)
version="v1"
docker build -f Dockerfile -t registry.cn-hangzhou.aliyuncs.com/sright-bcard/sright-bcard-enterprise:debug-$version .
docker push registry.cn-hangzhou.aliyuncs.com/sright-bcard/sright-bcard-enterprise:debug-$version

export KUBECONFIG=$(pwd)/config-bcard

kubectl set image deployment/sright-bcard-web-enterprise sright-bcard-web-enterprise=registry.cn-hangzhou.aliyuncs.com/sright-bcard/sright-bcard-enterprise:debug-$version

curl https://cs.console.aliyun.com/hook/trigger?token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbHVzdGVySWQiOiJjYzE5NDFjNzkwMmVkNDZmNDlhY2FiYzMwMjZjMzE5M2MiLCJpZCI6Ijk3ODgwIn0.GPEr_3fWewqiNV4uD5LJU4-wJTmN557gfkGVdx8qDtrA3y9CTbkF2rClvD3XHf5mArgo0poWVHViuMDV1H-Ag1_gndE5OXqO2l1iwqqHOvKpcHEPe4nLisz74VJu1h0ba-6RFctcFO7KjdKxN7XE5l2kqxjbfyw7Ghh6rXoDMl8

mirrorName="名片企业后台管理:sright-bcard-enterprise:debug-'${version}'"

#一直等待出现不是200的情况，就是新的容器启动，旧的容器被删除，但是容器中的应用还没有启动的时候
url="https://bcard-enterprise-dev-console.ergengtv.com/health"
code=`curl -I -m 30 -o /dev/null -s -w %{http_code}"\n" $url`
while [ $code -eq 200 ]; do
  code=`curl -I -m 30 -o /dev/null -s -w %{http_code}"\n" $url`
  echo "等待1秒"
  sleep 1
done
echo "容器已经切换，应用暂未启动"
# 出现不是200的情况，表明容器已经开始进行切换
i=1
# 40次120秒，2分钟，如果还不是200，那么就报错通知
while [ ${i} -le 40 ]
do
  code=`curl -I -m 30 -o /dev/null -s -w %{http_code}"\n" $url`
  if [ $code -eq 200 ]; then
    break
  fi
  echo "等待第'$i'个3秒"
  sleep 3s
  i=$[$i+1]
done
echo done

#再次变成200，那么表示新的容器中的应用已经启动成功
if [ $code -eq 200 ]; then
curl 'https://oapi.dingtalk.com/robot/send?access_token=bcee82b44d07eea97c7cb54b539a3e6dde5d48473e37dbda42b6c2cf446eaacc' \
-H 'Content-Type: application/json' \
-d '{
    "markdown": {
        "title":"jenkins发布 【发布成功】",
        "text":"- 镜像：'${mirrorName}'
           - 健康检查结果：'${code}'",
        "atMobiles": [
            "17702707574"
        ],
        "isAtAll": true
    },
    "msgtype": "markdown"
}'
else
curl 'https://oapi.dingtalk.com/robot/send?access_token=bcee82b44d07eea97c7cb54b539a3e6dde5d48473e37dbda42b6c2cf446eaacc' \
-H 'Content-Type: application/json' \
-d '{
    "markdown": {
        "title":"jenkins发布 【发布失败】",
        "text":"![error](https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=757328780,617021671&fm=26&gp=0.jpg)
           - 镜像：'${mirrorName}'
           - 健康检查结果：'${code}'",
        "atMobiles": [
            "17702707574"
        ],
        "isAtAll": true
    },
    "msgtype": "markdown"
}'
fi
